package ru.athena.runTool;

import java.awt.Component;
import java.awt.Container;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicDirectoryModel;
import javax.swing.plaf.basic.BasicFileChooserUI;

import sun.swing.WindowsPlacesBar;

@SuppressWarnings("serial")
public class CustomFileBrowser extends JFileChooser {

	public CustomFileBrowser() {	
		/* HACK: Leave only file system browser
		 * 
		 * */
		this.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES);
		for (Component c : getComponents()) {
			// WindowsPlacesBar; JToolBar; JPanel
			if ( (c instanceof JPanel )) {
				c.setVisible( true );
				Component[] children = ((JPanel)c).getComponents();
				// JPanel; JPanel; JPanel					
				if (children.length >= 3 ) {
					// 
					children[2].setVisible( false); 
				}
			} else if ( c instanceof WindowsPlacesBar ) {
				c.setVisible( false );   
			} else {
				c.setVisible( true );// "Parent folder" button, drop down list
			}
		} 
	}

	public void addChangeSelectionListener( PropertyChangeListener p) {
		addPropertyChangeListener( p);
	}
	
	/*
	private Component findJList(Component comp) {
	    if (comp.getClass() == JList.class) return comp;
	    if (comp instanceof Container) {
	        Component[] components = ((Container)comp).getComponents();
	        for(int i = 0; i < components.length; i++) {
	            Component child = findJList(components[i]);
	            if (child != null) return child;
	        }
	    }
	    return null;
	}*/
	
/*	private void setProperChangeListener() {
	   JFileChooser fch = (JFileChooser)this;
	   fch.addPropertyChangeListener( new PropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent evt) {
			
		}
		   
	   });
*/	   
/*	   BasicFileChooserUI ui = (BasicFileChooserUI)fch.getUI();
	   ui.createPropertyChangeListener(fch);
       BasicDirectoryModel model = ui.getModel();
        model.add)
        model.addListDataListener(new ListDataListener() {
            public void contentsChanged(ListDataEvent lde) {
                Vector buffer = model.getFiles();
                if (buffer.size() > 0) {
                    files = buffer;
                }
            }
            public void intervalAdded(ListDataEvent lde) {}
            public void intervalRemoved(ListDataEvent lde) {}
        });
    }	
*/    
}
